package com.cloudxingkong.dianyinke.Student.Ui;

import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.GridView;
import android.widget.TextView;

import com.cloudxingkong.dianyinke.NewTeacher.Utils.CookieHelper;
import com.cloudxingkong.dianyinke.R;
import com.cloudxingkong.dianyinke.Student.Adapter.CreditsExchangeGVAdapter;
import com.cloudxingkong.dianyinke.Student.bean.CETest;
import com.cloudxingkong.dianyinke.Student.bean.Iintegral;
import com.cloudxingkong.dianyinke.Uri.studentUri.StudentUri;
import com.cloudxingkong.dianyinke.Uri.teacherUri.ThisUri;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * 积分兑换Activity
 *
 * @ name Zxrui
 */
public class CreditsExchange extends BaseActivity {
    private List<CETest.ListBean> list;
    private List<Iintegral> lists;
    private GridView gridView;
    private CreditsExchangeGVAdapter gvAdapter;
    private SharedPreferences sharedPreferences;
    private TextView tv1, tv2;
    private ProgressDialog pro;

    @Override
    protected void onCreateView(Bundle savedInstanceState) {
        super.onCreateView(savedInstanceState);
        setContentView(R.layout.activity_credits_exchange);
        init();
        loadData();
        Iintegral();
    }

    private void Iintegral() {
        final String sessionid = CookieHelper.getSessionID(sharedPreferences);
        final String upLoadUrl = ThisUri.SERVER_YUMING + StudentUri.IINTEGRAL;
        RequestParams params = new RequestParams(upLoadUrl);
        params.addHeader("cookie", sessionid);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.e("text", "个人积分" + result);
                Message message = handler.obtainMessage();
                message.what = 2;
                message.obj = result;
                handler.sendMessage(message);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void loadData() {
        final String sessionid = CookieHelper.getSessionID(sharedPreferences);
        final String upLoadUrl = ThisUri.SERVER_YUMING + StudentUri.CREDITSEXCHANGE;
        RequestParams params = new RequestParams(upLoadUrl);
        params.addHeader("cookie", sessionid);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.e("text", "积分兑换" + result);
                Message message = handler.obtainMessage();
                message.what = 1;
                message.obj = result;
                handler.sendMessage(message);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void init() {
        sharedPreferences = CreditsExchange.this.getSharedPreferences("data_dyk", 0);
        gridView = (GridView) findViewById(R.id.ce_gridView);
        list = new ArrayList<>();
        gvAdapter = new CreditsExchangeGVAdapter(this, list);
        tv1 = ((TextView) findViewById(R.id.tv1));
        tv2 = ((TextView) findViewById(R.id.tv2));

    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    String state = (String) msg.obj;
                    if (state != null) {
                        Gson gson = new Gson();
                        CETest ceTest = gson.fromJson(state, CETest.class);
                        list.addAll(ceTest.getList());
                        gridView.setAdapter(gvAdapter);
                        gvAdapter.notifyDataSetChanged();
                        gvAdapter.notifyDataSetChanged();
                    }
                    break;
                case 2:
                    String states = (String) msg.obj;
                    if (states != null) {
                        Gson gson = new Gson();
                        Iintegral iintegral = gson.fromJson(states, Iintegral.class);
                        tv1.setText(iintegral.getObj().getHistpoints() + "");
                        tv2.setText(iintegral.getObj().getCurrpoints() + "");
                    }
            }

        }
    };

    //返回按钮
    public void back(View view) {
        finish();
    }
}
